//index.js
//获取应用实例
const app = getApp()
var bmap = require('../budu-map/bmap-wx.min.js');
var wxMarkerData = [];
// pages/coach/coach.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ak: "AXMRrsEZ0CGfogyRENeexOTkHxauhZtz",   //填写申请到的ak 
    indicatorColor:"white",
    indicatorActiveColor:"#3c92ff",
    imgUrls: [],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    address: '定位中',
    shop:[],
    vou:[],
    snum:0,
    keyword:''
  },
  learnCar:function(e){
    wx.navigateTo({
      url: "../learnCar/learnCar",
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },

  ruzhu: function () {
    wx.navigateTo({
      url: '../personal/personal',
    })
  },

  // 优惠劵

  jj: function (e) {
    var id = e.currentTarget.dataset.id;
    console.log(e);
    wx.request({
      url: app.d.ceshiUrl + '/Api/Voucher/get_voucher',
      data: {
        uid: app.d.userId,
        vid: id,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var status = res.data.status;
        if (status == 1) {
          wx.showToast({
            title: '已领取',
            icon: '',
            image: '',
            duration: 2000,
            mask: true,
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
          })
        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000,
          });
        }

      }
    })
  },
  
  cyclopedia: function (e){
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: '../cyclopedia/cyclopedia?id='+id+'&title='+title,
    })
  },

  ongGoToConsulting:function(){
    wx.navigateTo({
      url: '../consulting/consulting',
    })
  },

  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 定位
    var that = this;
    /* 获取定位地理位置 */
    // 新建bmap对象   
    var BMap = new bmap.BMapWX({
      ak: that.data.ak,
    });
    var fail = function (data) {
      console.log(data);
    };
    var success = function (data) {
      //返回数据内，已经包含经纬度  
      console.log(data);
      //使用wxMarkerData获取数据  
      wxMarkerData = data.originalData.result.addressComponent.city
      //把所有数据放在初始化data内  
      console.log(wxMarkerData)
      that.setData({
        address: wxMarkerData
      });
    }
    // 发起regeocoding检索请求   
    BMap.regeocoding({
      fail: fail,
      success: success
    });


    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Index/index',
      method: 'post',
      data: {},
      header: {
        'content-type': 'application/json',
      },
      success: function (res) {
        var ggtop = res.data.ggtop;
        var snum = res.data.snum;
        var vou = res.data.vou;
        var shop = res.data.shop;
        //that.initProductData(data);
        that.setData({
          imgUrls: ggtop,
          shop: shop,
          snum: snum,
          vou: vou,
        });
        //endInitData
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })
  },

  search:function (e){
    var that = this;
    var keyword = e.detail.value;
    that.setData({
      keyword : keyword
    });
    wx.request({
      url: app.d.ceshiUrl + '/Api/Shangchang/index',
      method: 'post',
      data: {
        keyword: keyword
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shop: res.data.store_list
        });
        //endInitData
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '东莞学车练车',
      path: '/pages/index/index',
      success: function (res) {
        // 分享成功
      },
      fail: function (res) {
        // 分享失败
      }
    }
  }
})